// pages/register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  usernameInput: function (e) {
    this.setData({
      userID: e.detail.value
    })
  },

  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },

  repasswordInput: function(e) {
    this.setData({
      repassword: e.detail.value
    })
  },

  nameInput: function(e) {
    this.setData({
      name: e.detail.value
    })
  },

  IDcardInput: function(e) {
    this.setData({
      IDcard: e.detail.value
    })
  },

  phoneInput: function(e) {
    this.setData({
      tel: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  registAction: function() {

    if(this.data.userID == null) {
      wx.showToast({
        title: '请输入用户名',
        image: '/images/x1.png'
      })
    }
    else {
      if(this.data.password == null) {
        wx.showToast({
          title: '请输入密码',
          image: '/images/x1.png'
        })
      }
      else {
        if(this.data.password != this.data.repassword) {
          wx.showToast({
            title: '两次密码不一致',
            image: '/images/x1.png'
          })
        }
        else {
          wx.request({
            url: 'http://localhost:8080/TrainManager/RegisterUser',
            data: {
              userID: this.data.userID,
              password: this.data.password,
              name: this.data.name,
              IDcard: this.data.IDcard,
              tel: this.data.tel
            },
    
            success: (res) => {          
              if(res.data == '0') {
                wx.showToast({
                  title: '当前用户已存在',
                  image: '/images/x1.png'
                })
              }
              else {
                wx.showToast({
                  title: '注册成功！',
                  duration: 1000,
                  success: function(){
                    setTimeout( function(){
                      wx.navigateBack({
                        delta: 1,
                      })
                    }, 1000)
                  }
                })
              }
            }
          })
        }
      }
    }
  }
})